Artificial Intelligence Nanodegree

Computer Vision Capstone

Project: Facial Keypoint Detection


Welcome to the final Computer Vision project in the Artificial Intelligence Nanodegree program!

In this project, you’ll combine your knowledge of computer vision techniques and deep learning to build and end-to-end facial keypoint recognition system! Facial keypoints include points around the eyes, nose, and mouth on any face and are used in many applications, from facial tracking to emotion recognition.

There are three main parts to this project:

Part 1 : Investigating OpenCV, pre-processing, and face detection

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!


*Here's what you need to know to complete the project:

  1. In this notebook, some template code has already been provided for you, and you will need to implement additional functionality to successfully complete this project. You will not need to modify the included code beyond what is requested.

    a. Sections that begin with '(IMPLEMENTATION)' in the header indicate that the following block of code will require additional functionality which you must provide. Instructions will be provided for each section, and the specifics of the implementation are marked in the code block with a 'TODO' statement. Please be sure to read the instructions carefully!

  1. In addition to implementing code, there will be questions that you must answer which relate to the project and your implementation.

    a. Each section where you will answer a question is preceded by a 'Question X' header.

    b. Carefully read each question and provide thorough answers in the following text boxes that begin with 'Answer:'.

Note: Code and Markdown cells can be executed using the Shift + Enter keyboard shortcut. Markdown cells can be edited by double-clicking the cell to enter edit mode.

The rubric contains optional suggestions for enhancing the project beyond the minimum requirements. If you decide to pursue the "(Optional)" sections, you should include the code in this IPython notebook.

Your project submission will be evaluated based on your answers to each of the questions and the code implementations you provide.

Steps to Complete the Project

Each part of the notebook is further broken down into separate steps. Feel free to use the links below to navigate the notebook.

In this project you will get to explore a few of the many computer vision algorithms built into the OpenCV library. This expansive computer vision library is now almost 20 years old and still growing!

The project itself is broken down into three large parts, then even further into separate steps. Make sure to read through each step, and complete any sections that begin with '(IMPLEMENTATION)' in the header; these implementation sections may contain multiple TODOs that will be marked in code. For convenience, we provide links to each of these steps below.

Part 1 : Investigating OpenCV, pre-processing, and face detection

  • Step 0: Detect Faces Using a Haar Cascade Classifier
  • Step 1: Add Eye Detection
  • Step 2: De-noise an Image for Better Face Detection
  • Step 3: Blur an Image and Perform Edge Detection
  • Step 4: Automatically Hide the Identity of an Individual

Part 2 : Training a Convolutional Neural Network (CNN) to detect facial keypoints

  • Step 5: Create a CNN to Recognize Facial Keypoints
  • Step 6: Compile and Train the Model
  • Step 7: Visualize the Loss and Answer Questions

Part 3 : Putting parts 1 and 2 together to identify facial keypoints on any image!

  • Step 8: Build a Robust Facial Keypoints Detector (Complete the CV Pipeline)

Step 0: Detect Faces Using a Haar Cascade Classifier

Have you ever wondered how Facebook automatically tags images with your friends' faces? Or how high-end cameras automatically find and focus on a certain person's face? Applications like these depend heavily on the machine learning task known as face detection - which is the task of automatically finding faces in images containing people.

At its root face detection is a classification problem - that is a problem of distinguishing between distinct classes of things. With face detection these distinct classes are 1) images of human faces and 2) everything else.

We use OpenCV's implementation of Haar feature-based cascade classifiers to detect human faces in images. OpenCV provides many pre-trained face detectors, stored as XML files on github. We have downloaded one of these detectors and stored it in the detector_architectures directory.

Import Resources

In the next python cell, we load in the required libraries for this section of the project.

In [2]:
!conda update -n base conda --yes
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs: 
    - conda


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2018.4.16          |           py35_0         143 KB

The following packages will be UPDATED:

    certifi:         2018.4.16-py35_0 conda-forge --> 2018.4.16-py35_0 
    conda:           4.5.3-py35_0     conda-forge --> 4.5.4-py35_0     
    openssl:         1.0.2o-0         conda-forge --> 1.0.2o-h20670df_0

The following packages will be DOWNGRADED:

    ca-certificates: 2018.4.16-0      conda-forge --> 2018.03.07-0     


Downloading and Extracting Packages
certifi 2018.4.16####################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
In [3]:
!conda update --all --yes
Solving environment: done

## Package Plan ##

  environment location: /opt/conda


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    freeglut-3.0.0             |       hf484d3e_5         251 KB
    jasper-1.900.1             |       hd497a04_4         279 KB
    opencv-3.3.1               |   py35hb7523de_0        38.9 MB
    ------------------------------------------------------------
                                           Total:        39.4 MB

The following packages will be UPDATED:

    cairo:               1.14.6-4              conda-forge --> 1.14.12-h7636065_2   
    dbus:                1.11.0-0              conda-forge --> 1.13.2-h714fa37_1    
    fontconfig:          2.12.1-4              conda-forge --> 2.12.6-h49f89f6_0    
    freeglut:            2.8.1-0                           --> 3.0.0-hf484d3e_5     
    freetype:            2.7-1                 conda-forge --> 2.8-hab7d2ae_1       
    glib:                2.51.4-0              conda-forge --> 2.56.1-h000015b_0    
    gst-plugins-base:    1.8.0-0               conda-forge --> 1.14.0-hbbd80ab_1    
    gstreamer:           1.8.0-2               conda-forge --> 1.14.0-hb453b48_1    
    h5py:                2.7.0-np112py35_0     conda-forge --> 2.7.1-py35h8d53cdc_0 
    harfbuzz:            1.3.4-2               conda-forge --> 1.7.6-h5f0a787_1     
    hdf5:                1.8.17-11             conda-forge --> 1.10.1-h9caa474_1    
    matplotlib:          2.1.0-py35_0          conda-forge --> 2.2.2-py35h0e671d2_1 
    numpy:               1.12.1-py35hca0bb5e_1             --> 1.14.2-py35hdbf6ddf_1
    opencv:              3.2.0-np112py35_0     conda-forge --> 3.3.1-py35hb7523de_0 
    pango:               1.40.3-3              conda-forge --> 1.41.0-hd475d92_0    
    pillow:              4.2.1-py35_0          conda-forge --> 5.1.0-py35h3deb7b8_0 
    pyqt:                5.6.0-py35_2                      --> 5.9.2-py35h751905a_0 
    pytables:            3.3.0-np112py35_0     conda-forge --> 3.4.3-py35h02b9ad4_0 
    python:              3.5.4-0                           --> 3.5.5-hc3d631a_4     
    qt:                  5.6.2-3               conda-forge --> 5.9.5-h7e424d6_0     
    readline:            6.2-2                             --> 7.0-ha6073c6_4       
    scikit-image:        0.13.1-py35_0         conda-forge --> 0.13.1-py35h14c3975_1
    sqlite:              3.13.0-1              conda-forge --> 3.23.1-he433501_0    
    tensorflow-gpu:      1.5.0-0                           --> 1.7.0-0              
    tensorflow-gpu-base: 1.5.0-py35h8a131e3_0              --> 1.7.0-py35hcdda91b_1 
    tk:                  8.5.18-0                          --> 8.6.7-hc745277_3     

The following packages will be DOWNGRADED:

    jasper:              2.0.14-h07fcdf6_0                 --> 1.900.1-hd497a04_4   


Downloading and Extracting Packages
freeglut-3.0.0       |  251 KB | ####################################### | 100% 
jasper-1.900.1       |  279 KB | ####################################### | 100% 
opencv-3.3.1         | 38.9 MB | ####################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
In [4]:
!apt-get install libopenblas-dev -y
!conda install -c conda-forge opencv=3.2.0 --yes
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libopenblas-dev is already the newest version (0.2.18-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs: 
    - opencv=3.2.0


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2018.4.16          |           py35_0         142 KB  conda-forge

The following packages will be UPDATED:

    ca-certificates:     2018.03.07-0          --> 2018.4.16-0           conda-forge
    certifi:             2018.4.16-py35_0      --> 2018.4.16-py35_0      conda-forge
    openssl:             1.0.2o-h20670df_0     --> 1.0.2o-0              conda-forge

The following packages will be DOWNGRADED:

    cairo:               1.14.12-h7636065_2    --> 1.14.6-4              conda-forge
    conda:               4.5.4-py35_0          --> 4.5.3-py35_0          conda-forge
    dbus:                1.13.2-h714fa37_1     --> 1.11.0-0              conda-forge
    fontconfig:          2.12.6-h49f89f6_0     --> 2.12.1-4              conda-forge
    freetype:            2.8-hab7d2ae_1        --> 2.7-1                 conda-forge
    glib:                2.56.1-h000015b_0     --> 2.51.4-0              conda-forge
    gst-plugins-base:    1.14.0-hbbd80ab_1     --> 1.8.0-0               conda-forge
    gstreamer:           1.14.0-hb453b48_1     --> 1.8.0-2               conda-forge
    h5py:                2.7.1-py35h8d53cdc_0  --> 2.7.0-np112py35_0     conda-forge
    harfbuzz:            1.7.6-h5f0a787_1      --> 1.3.4-2               conda-forge
    hdf5:                1.10.1-h9caa474_1     --> 1.8.17-11             conda-forge
    matplotlib:          2.2.2-py35h0e671d2_1  --> 2.1.0-py35_0          conda-forge
    numpy:               1.14.2-py35hdbf6ddf_1 --> 1.12.1-py35hca0bb5e_1            
    opencv:              3.3.1-py35hb7523de_0  --> 3.2.0-np112py35_0     conda-forge
    pango:               1.41.0-hd475d92_0     --> 1.40.3-3              conda-forge
    pillow:              5.1.0-py35h3deb7b8_0  --> 4.2.1-py35_0          conda-forge
    pyqt:                5.9.2-py35h751905a_0  --> 5.6.0-py35_2                     
    pytables:            3.4.3-py35h02b9ad4_0  --> 3.3.0-np112py35_0     conda-forge
    python:              3.5.5-hc3d631a_4      --> 3.5.4-0                          
    qt:                  5.9.5-h7e424d6_0      --> 5.6.2-3               conda-forge
    readline:            7.0-ha6073c6_4        --> 6.2-2                            
    scikit-image:        0.13.1-py35h14c3975_1 --> 0.13.1-py35_0         conda-forge
    sqlite:              3.23.1-he433501_0     --> 3.13.0-1              conda-forge
    tensorflow-gpu:      1.7.0-0               --> 1.5.0-0                          
    tensorflow-gpu-base: 1.7.0-py35hcdda91b_1  --> 1.5.0-py35h8a131e3_0             
    tk:                  8.6.7-hc745277_3      --> 8.5.18-0                         


Downloading and Extracting Packages
certifi-2018.4.16    |  142 KB | ####################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
In [5]:
!conda list
# packages in environment at /opt/conda:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0            py35h02fa31e_0  
_nb_ext_conf              0.4.0                    py35_1  
absl-py                   0.2.0                    py35_0  
alabaster                 0.7.10           py35h6fb19ab_0  
anaconda                  custom           py35h104c396_0  
anaconda-client           1.6.14                   py35_0  
anaconda-navigator        1.8.5                    py35_0  
anaconda-project          0.8.2            py35h7d62c80_0  
asn1crypto                0.24.0                   py35_0  
astor                     0.6.2                    py35_0  
astroid                   1.6.3                    py35_0  
astropy                   3.0.2            py35h3010b51_1  
attrs                     18.1.0                   py35_0  
babel                     2.5.3                    py35_0  
backcall                  0.1.0                    py35_0  
backports                 1.0              py35hd471ac7_1  
backports.functools_lru_cache 1.5                      py35_0    conda-forge
backports.shutil_get_terminal_size 1.0.0            py35h40844db_2  
beautifulsoup4            4.6.0            py35h442a8c9_1  
bitarray                  0.8.1            py35h14c3975_1  
bkcharts                  0.2              py35he4f7e30_0  
blaze                     0.11.3           py35ha2c9137_0  
bleach                    1.5.0                    py35_0  
blosc                     1.14.3               hdbcaa40_0  
bokeh                     0.12.16                  py35_0  
boto                      2.48.0           py35h2cfd601_1  
bottleneck                1.2.1            py35he1b16f1_0  
bzip2                     1.0.6                h9a117a8_4  
ca-certificates           2018.4.16                     0    conda-forge
cairo                     1.14.6                        4    conda-forge
certifi                   2018.4.16                py35_0    conda-forge
cffi                      1.11.5           py35h9745a5d_0  
chardet                   3.0.4            py35hb6e9ddf_1  
click                     6.7              py35h353a69f_0  
cloudpickle               0.5.3                    py35_0  
clyent                    1.2.2            py35h491ffcb_1  
colorama                  0.3.9            py35h81e2b6c_0  
conda                     4.5.3                    py35_0    conda-forge
conda-build               3.10.4                   py35_0  
conda-env                 2.6.0                h36134e3_1  
conda-verify              2.0.0            py35he912c7b_0  
contextlib2               0.5.5            py35h6690dba_0  
cryptography              2.2.2            py35h14c3975_0  
cudatoolkit               9.0                  h13b8566_0  
cudnn                     7.1.2                 cuda9.0_0  
cupti                     9.0.176                       0  
curl                      7.59.0               h84994c4_0  
cycler                    0.10.0           py35hc4d5149_0  
cython                    0.28.2           py35h14c3975_0  
cytoolz                   0.9.0.1          py35h14c3975_0  
dask                      0.17.5                   py35_0  
dask-core                 0.17.5                   py35_0  
datashape                 0.5.4            py35h5b068ae_0  
dbus                      1.11.0                        0    conda-forge
decorator                 4.3.0                    py35_0  
distributed               1.21.8                   py35_0  
docutils                  0.14             py35hd11081d_0  
entrypoints               0.2.3            py35h48174a2_2  
et_xmlfile                1.0.1            py35hde8d43e_0  
expat                     2.2.5                he0dffb1_0  
fastcache                 1.0.2            py35h14c3975_2  
ffmpeg                    3.4                  h7264315_0  
filelock                  3.0.4                    py35_0  
flask                     1.0.2                    py35_1  
flask-cors                3.0.4                    py35_0  
fontconfig                2.12.1                        4    conda-forge
freeglut                  3.0.0                hf484d3e_5  
freetype                  2.7                           1    conda-forge
future                    0.16.0                    <pip>
gast                      0.2.0                    py35_0  
get_terminal_size         1.0.0                haa9412d_0  
gettext                   0.19.8.1             hd7bead4_3  
gevent                    1.3.0            py35h14c3975_0  
giflib                    5.1.4                h26a3ec6_1  
glib                      2.51.4                        0    conda-forge
glob2                     0.6              py35hff66265_0  
gmp                       6.1.2                h6c8ec71_1  
gmpy2                     2.0.8            py35hd0a1c9a_2  
graphite2                 1.3.11               hf63cedd_1  
greenlet                  0.4.13           py35h14c3975_0  
grpcio                    1.11.0           py35hf484d3e_0  
gst-plugins-base          1.8.0                         0    conda-forge
gstreamer                 1.8.0                         2    conda-forge
h5py                      2.7.0               np112py35_0    conda-forge
harfbuzz                  1.3.4                         2    conda-forge
hdf5                      1.8.17                       11    conda-forge
heapdict                  1.0.0                    py35_2  
hmmlearn                  0.2.1                     <pip>
html5lib                  0.9999999                py35_0  
icu                       58.2                 h9c2bf20_1  
idna                      2.6              py35h8605a33_1  
imageio                   2.3.0                    py35_0  
imagesize                 1.0.0                    py35_0  
intel-openmp              2018.0.0                      8  
ipykernel                 4.8.2                    py35_0  
ipython                   6.4.0                    py35_0  
ipython_genutils          0.2.0            py35hc9e07d0_0  
ipywidgets                7.2.1                    py35_0  
isort                     4.3.4                    py35_0  
itsdangerous              0.24             py35h7c46880_1  
jasper                    1.900.1              hd497a04_4  
jbig                      2.1                  hdba287a_0  
jdcal                     1.4                      py35_0  
jedi                      0.12.0                   py35_1  
jinja2                    2.10             py35h480ab6d_0  
jpeg                      9b                   h024ee3a_2  
jsonschema                2.6.0            py35h4395190_0  
jupyter                   1.0.0                    py35_4  
jupyter_client            5.2.3                    py35_0  
jupyter_console           5.2.0            py35h4044a63_1  
jupyter_core              4.4.0            py35ha89e94b_0  
jupyterlab                0.32.1                   py35_0  
jupyterlab_launcher       0.10.5                   py35_0  
Keras                     2.1.3                     <pip>
kiwisolver                1.0.1            py35hcb1117a_0  
lazy-object-proxy         1.3.1            py35h4c720c6_0  
libcurl                   7.59.0               h1ad7b7a_0  
libedit                   3.1.20170329         h6b74fdf_2  
libffi                    3.2.1                hd88cf55_4  
libgcc                    7.2.0                h69d50b8_2  
libgcc-ng                 7.2.0                hdf63c60_3  
libgfortran               3.0.0                         1  
libgfortran-ng            7.2.0                hdf63c60_3  
libglu                    9.0.0                h0c0bdc1_1  
libiconv                  1.15                 h63c8f33_5  
libopus                   1.2.1                hb9ed12e_0  
libpng                    1.6.34               hb9fc6fc_0  
libprotobuf               3.5.2                h6f1eeef_0  
libsodium                 1.0.16               h1bed415_0  
libssh2                   1.8.0                h9cfc8f7_4  
libstdcxx-ng              7.2.0                hdf63c60_3  
libtiff                   4.0.9                he85c1e1_1  
libtool                   2.4.6                h544aabb_3  
libvpx                    1.6.1                h888fd40_0  
libwebp                   0.5.2                         7    conda-forge
libxcb                    1.13                 h1bed415_1  
libxml2                   2.9.8                hf84eae3_0  
libxslt                   1.1.32               h1312cb7_0  
llvmlite                  0.23.1           py35hdbcaa40_0  
locket                    0.2.0            py35h170bc82_1  
lxml                      4.2.1            py35h23eabaa_0  
lzo                       2.10                 h49e0be7_2  
markdown                  2.6.11                   py35_0  
markupsafe                1.0              py35h4f4fcf6_1  
matplotlib                2.1.0                    py35_0    conda-forge
mccabe                    0.6.1            py35hd8c1f23_1  
mistune                   0.8.3            py35h14c3975_1  
mkl                       2018.0.2                      1  
mkl-service               1.1.2            py35h0fc7090_4  
mkl_fft                   1.0.1            py35h3010b51_0  
mkl_random                1.0.1            py35h629b387_0  
more-itertools            4.1.0                    py35_0  
mpc                       1.0.3                hec55b23_5  
mpfr                      3.1.5                h11a74b3_2  
mpmath                    1.0.0            py35h7ce6e34_2  
msgpack-python            0.5.6            py35h6bb024c_0  
multipledispatch          0.5.0                    py35_0  
navigator-updater         0.2.0                    py35_0  
nb_anacondacloud          1.4.0                    py35_0  
nb_conda                  2.2.1            py35hccc8299_0  
nb_conda_kernels          2.1.0                    py35_0  
nbconvert                 5.3.1            py35hc5194e3_0  
nbformat                  4.4.0            py35h12e6e07_0  
nbpresent                 3.0.2            py35h9c03491_1  
ncurses                   6.1                  hf484d3e_0  
networkx                  2.1                      py35_0  
nltk                      3.3.0                    py35_0  
nose                      1.3.7            py35hdc64897_2  
notebook                  5.5.0                    py35_0  
numba                     0.38.0           py35h637b7d7_0  
numexpr                   2.6.5            py35h7bf3b9c_0  
numpy                     1.12.1           py35hca0bb5e_1  
numpy-base                1.14.3           py35hdbf6ddf_0  
numpydoc                  0.8.0                    py35_0  
odo                       0.5.1            py35h102315f_0  
olefile                   0.45.1                   py35_0  
opencv                    3.2.0               np112py35_0    conda-forge
openpyxl                  2.5.3                    py35_0  
openssl                   1.0.2o                        0    conda-forge
packaging                 17.1                     py35_0  
pandas                    0.23.0           py35h637b7d7_0  
pandoc                    1.19.2.1             hea2e7c5_1  
pandocfilters             1.4.2            py35h1565a15_1  
pango                     1.40.3                        3    conda-forge
parso                     0.2.0                    py35_0  
partd                     0.3.8            py35h68187f2_0  
patchelf                  0.9                  hf79760b_2  
path.py                   11.0.1                   py35_0  
pathlib2                  2.3.2                    py35_0  
patsy                     0.5.0                    py35_0  
pcre                      8.42                 h439df22_0  
pep8                      1.7.1                    py35_0  
pexpect                   4.5.0                    py35_0  
pickleshare               0.7.4            py35hd57304d_0  
pillow                    4.2.1                    py35_0    conda-forge
pip                       10.0.1                   py35_0  
pixman                    0.34.0               hceecf20_3  
pkginfo                   1.4.2                    py35_1  
pluggy                    0.6.0            py35hb428803_0  
ply                       3.11                     py35_0  
prompt_toolkit            1.0.15           py35hc09de7a_0  
protobuf                  3.5.2            py35hf484d3e_0  
psutil                    5.4.5            py35h14c3975_0  
ptyprocess                0.5.2            py35h38ce0a3_0  
py                        1.5.3                    py35_0  
pycodestyle               2.4.0                    py35_0  
pycosat                   0.6.3            py35h6b6bb97_0  
pycparser                 2.18             py35h61b3040_1  
pycrypto                  2.6.1            py35h14c3975_8  
pycurl                    7.43.0.1         py35hb7f436b_0  
pyflakes                  1.6.0            py35hd1da72e_0  
pygments                  2.2.0            py35h0f41973_0  
pylint                    1.8.4                    py35_0  
pyodbc                    4.0.23           py35hf484d3e_0  
pyopenssl                 18.0.0                   py35_0  
pyparsing                 2.2.0            py35h041ed72_1  
pyqt                      5.6.0                    py35_2  
pysocks                   1.6.8                    py35_0  
pytables                  3.3.0               np112py35_0    conda-forge
pytest                    3.5.1                    py35_0  
pytest-arraydiff          0.2                      py35_0  
pytest-astropy            0.3.0                    py35_0  
pytest-doctestplus        0.1.3                    py35_0  
pytest-openfiles          0.3.0                    py35_0  
pytest-remotedata         0.2.1                    py35_0  
python                    3.5.4                         0  
python-dateutil           2.7.3                    py35_0  
pytz                      2018.4                   py35_0  
pywavelets                0.5.2            py35h53ec731_0  
pyyaml                    3.12             py35h46ef4ae_1  
pyzmq                     17.0.0           py35h14c3975_0  
qt                        5.6.2                         3    conda-forge
qtawesome                 0.4.4            py35hd68ff89_0  
qtconsole                 4.3.1            py35h4626a06_0  
qtpy                      1.4.1                    py35_0  
readline                  6.2                           2  
requests                  2.18.4           py35hb9e6ad1_1  
requests-toolbelt         0.8.0                     <pip>
rope                      0.10.7           py35h986144c_0  
rpy2                      2.9.2                     <pip>
ruamel_yaml               0.15.35          py35h14c3975_1  
scikit-image              0.13.1                   py35_0    conda-forge
scikit-learn              0.19.1           py35hbf1f462_0  
scipy                     1.1.0            py35hfc37229_0  
seaborn                   0.8.1            py35h04cba02_0  
send2trash                1.5.0                    py35_0  
setuptools                39.1.0                   py35_0  
simplegeneric             0.8.1                    py35_2  
singledispatch            3.4.0.3          py35h0cd4ec3_0  
sip                       4.19.8           py35hf484d3e_0  
six                       1.11.0           py35h423b573_1  
snappy                    1.1.7                hbae5bb6_3  
snowballstemmer           1.2.1            py35h5435977_0  
sortedcollections         0.6.1                    py35_0  
sortedcontainers          1.5.10                   py35_0  
sphinx                    1.7.4                    py35_0  
sphinxcontrib             1.0              py35hcb7efd2_1  
sphinxcontrib-websupport  1.0.1            py35h3537809_1  
spyder                    3.2.8                    py35_0  
sqlalchemy                1.2.7            py35h6b74fdf_0  
sqlite                    3.13.0                        1    conda-forge
statsmodels               0.9.0            py35h3010b51_0  
sympy                     1.1.1            py35h919b29a_0  
tblib                     1.3.2            py35hf1eb0b4_0  
tensorboard               1.7.0            py35hf484d3e_1  
tensorflow-gpu            1.5.0                         0  
tensorflow-gpu-base       1.5.0            py35h8a131e3_0  
tensorflow-tensorboard    1.5.1            py35hf484d3e_1  
termcolor                 1.1.0                    py35_1  
terminado                 0.8.1                    py35_1  
testpath                  0.3.1            py35had42eaf_0  
tk                        8.5.18                        0  
toolz                     0.9.0                    py35_0  
tornado                   5.0.2                    py35_0  
tqdm                      4.23.0                   py35_0  
traitlets                 4.3.2            py35ha522a97_0  
typing                    3.6.4                    py35_0  
udacity-pa                0.2.6                     <pip>
unicodecsv                0.14.1           py35h4805c0c_0  
unixodbc                  2.3.6                h1bed415_0  
urllib3                   1.22             py35h2ab6e29_0  
wcwidth                   0.1.7            py35hcd08066_0  
webencodings              0.5.1            py35hb6cf162_1  
werkzeug                  0.14.1                   py35_0  
wheel                     0.31.1                   py35_0  
widgetsnbextension        3.2.1                    py35_0  
wrapt                     1.10.11          py35hfdafd39_0  
xlrd                      1.1.0            py35h45a0a2a_1  
xlsxwriter                1.0.4                    py35_0  
xlwt                      1.3.0            py35h884a0cf_0  
xz                        5.2.4                h14c3975_4  
yaml                      0.1.7                had09818_2  
z3-solver                 4.5.1.0.post2             <pip>
zeromq                    4.2.5                h439df22_0  
zict                      0.1.3            py35h29275ca_0  
zlib                      1.2.11               ha838bed_2  
In [1]:
# Import required libraries for this section

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import math
import cv2                     # OpenCV library for computer vision
from PIL import Image
import time 

Next, we load in and display a test image for performing face detection.

Note: by default OpenCV assumes the ordering of our image's color channels are Blue, then Green, then Red. This is slightly out of order with most image types we'll use in these experiments, whose color channels are ordered Red, then Green, then Blue. In order to switch the Blue and Red channels of our test image around we will use OpenCV's cvtColor function, which you can read more about by checking out some of its documentation located here. This is a general utility function that can do other transformations too like converting a color image to grayscale, and transforming a standard color image to HSV color space.

In [2]:
# Load in color image for face detection
image = cv2.imread('images/test_image_1.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot our image using subplots to specify a size and title
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[2]:
<matplotlib.image.AxesImage at 0x7fc3cb493cf8>

There are a lot of people - and faces - in this picture. 13 faces to be exact! In the next code cell, we demonstrate how to use a Haar Cascade classifier to detect all the faces in this test image.

This face detector uses information about patterns of intensity in an image to reliably detect faces under varying light conditions. So, to use this face detector, we'll first convert the image from color to grayscale.

Then, we load in the fully trained architecture of the face detector -- found in the file haarcascade_frontalface_default.xml - and use it on our image to find faces!

To learn more about the parameters of the detector see this post.

In [3]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 4, 6)

print(faces)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detections')
ax1.imshow(image_with_detections)
[[1295   94   96   96]
 [ 917  103   96   96]
 [ 684  150   96   96]
 [ 510  156   96   96]
 [1146  132   96   96]
 [1565  339   96   96]
 [ 588  390   96   96]
 [1158  391   96   96]
 [ 773  404   96   96]
 [ 383  414   96   96]
 [ 147  459   96   96]
 [1345  411   96   96]
 [ 996  526   96   96]]
Number of faces detected: 13
Out[3]:
<matplotlib.image.AxesImage at 0x7fc3cb476240>

In the above code, faces is a numpy array of detected faces, where each row corresponds to a detected face. Each detected face is a 1D array with four entries that specifies the bounding box of the detected face. The first two entries in the array (extracted in the above code as x and y) specify the horizontal and vertical positions of the top left corner of the bounding box. The last two entries in the array (extracted here as w and h) specify the width and height of the box.


Step 1: Add Eye Detections

There are other pre-trained detectors available that use a Haar Cascade Classifier - including full human body detectors, license plate detectors, and more. A full list of the pre-trained architectures can be found here.

To test your eye detector, we'll first read in a new test image with just a single face.

In [4]:
# Load in color image for face detection
image = cv2.imread('images/james.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Plot the RGB image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)
Out[4]:
<matplotlib.image.AxesImage at 0x7fc3cb424358>

Notice that even though the image is a black and white image, we have read it in as a color image and so it will still need to be converted to grayscale in order to perform the most accurate face detection.

So, the next steps will be to convert this image to grayscale, then load OpenCV's face detector and run it with parameters that detect this face accurately.

In [5]:
# Convert the RGB  image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray, 1.25, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face Detection')
ax1.imshow(image_with_detections)
Number of faces detected: 1
Out[5]:
<matplotlib.image.AxesImage at 0x7fc38999b470>

(IMPLEMENTATION) Add an eye detector to the current face detection setup.

A Haar-cascade eye detector can be included in the same way that the face detector was and, in this first task, it will be your job to do just this.

To set up an eye detector, use the stored parameters of the eye cascade detector, called haarcascade_eye.xml, located in the detector_architectures subdirectory. In the next code cell, create your eye detector and store its detections.

A few notes before you get started:

First, make sure to give your loaded eye detector the variable name

eye_cascade

and give the list of eye regions you detect the variable name

eyes

Second, since we've already run the face detector over this image, you should only search for eyes within the rectangular face regions detected in faces. This will minimize false detections.

Lastly, once you've run your eye detector over the facial detection region, you should display the RGB image with both the face detection boxes (in red) and your eye detections (in green) to verify that everything works as expected.

In [66]:
# Make a copy of the original image to plot rectangle detections
image_with_detections = np.copy(image)   

# Loop over the detections and draw their corresponding face detection boxes
for (x,y,w,h) in faces:
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h),(255,0,0), 3)  
    
# Do not change the code above this comment!


## TODO: Add eye detection, using haarcascade_eye.xml, to the current face detector algorithm
eye_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_eye.xml')
eyes = []
for (f_x,f_y,f_w,f_h) in faces:
    face_image = image[f_y:f_y+f_h, f_x:f_x+f_w]
    # plt.imshow(face_image)
    eyes_in_face = eye_cascade.detectMultiScale(face_image, 1.2, 3)
    for (e_x,e_y,e_w,e_h) in eyes_in_face:
        eyes.append( ( (f_x+e_x, f_y+e_y), (f_x+e_x+e_w, f_y+e_y+e_h) ) )
    
## TODO: Loop over the eye detections and draw their corresponding boxes in green on image_with_detections
for (e_x_y,e_w_h) in eyes:
    cv2.rectangle(image_with_detections, e_x_y, e_w_h, (0,255,0), 3)


# Plot the image with both faces and eyes detected
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Image with Face and Eye Detection')
ax1.imshow(image_with_detections)
Out[66]:
<matplotlib.image.AxesImage at 0x7fc36bca7d30>

I tried alot of parameters, it is hard to get them all perfect

(Optional) Add face and eye detection to your laptop camera

I am running this notebook inside a docker container, so I have no access to a camera

It's time to kick it up a notch, and add face and eye detection to your laptop's camera! Afterwards, you'll be able to show off your creation like in the gif shown below - made with a completed version of the code!

Notice that not all of the detections here are perfect - and your result need not be perfect either. You should spend a small amount of time tuning the parameters of your detectors to get reasonable results, but don't hold out for perfection. If we wanted perfection we'd need to spend a ton of time tuning the parameters of each detector, cleaning up the input image frames, etc. You can think of this as more of a rapid prototype.

The next cell contains code for a wrapper function called laptop_camera_face_eye_detector that, when called, will activate your laptop's camera. You will place the relevant face and eye detection code in this wrapper function to implement face/eye detection and mark those detections on each image frame that your camera captures.

Before adding anything to the function, you can run it to get an idea of how it works - a small window should pop up showing you the live feed from your camera; you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [ ]:
### Add face and eye detection to this laptop camera function 
# Make sure to draw out all faces/eyes found in each frame on the shown video feed

import cv2
import time 

# wrapper function for face/eye detection with your laptop camera
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep the video stream open
    while rval:
        # Plot the image from camera with all the face and eye detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            # Make sure window closes on OSx
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
In [ ]:
# Call the laptop camera face/eye detector function above
laptop_camera_go()

Step 2: De-noise an Image for Better Face Detection

Image quality is an important aspect of any computer vision task. Typically, when creating a set of images to train a deep learning network, significant care is taken to ensure that training images are free of visual noise or artifacts that hinder object detection. While computer vision algorithms - like a face detector - are typically trained on 'nice' data such as this, new test data doesn't always look so nice!

When applying a trained computer vision algorithm to a new piece of test data one often cleans it up first before feeding it in. This sort of cleaning - referred to as pre-processing - can include a number of cleaning phases like blurring, de-noising, color transformations, etc., and many of these tasks can be accomplished using OpenCV.

In this short subsection we explore OpenCV's noise-removal functionality to see how we can clean up a noisy image, which we then feed into our trained face detector.

Create a noisy image to work with

In the next cell, we create an artificial noisy version of the previous multi-face image. This is a little exaggerated - we don't typically get images that are this noisy - but image noise, or 'grainy-ness' in a digitial image - is a fairly common phenomenon.

In [49]:
# Load in the multi-face test image again
image = cv2.imread('images/test_image_1.jpg')

# Convert the image copy to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Make an array copy of this image
image_with_noise = np.asarray(image)

# Create noise - here we add noise sampled randomly from a Gaussian distribution: a common model for noise
noise_level = 40
noise = np.random.randn(image.shape[0],image.shape[1],image.shape[2])*noise_level

# Add this noise to the array image copy
image_with_noise = image_with_noise + noise

# Convert back to uint8 format
image_with_noise = np.asarray([np.uint8(np.clip(i,0,255)) for i in image_with_noise])

# Plot our noisy image!
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Noisy Image')
ax1.imshow(image_with_noise)
Out[49]:
<matplotlib.image.AxesImage at 0x7fc36bec2eb8>

In the context of face detection, the problem with an image like this is that - due to noise - we may miss some faces or get false detections.

In the next cell we apply the same trained OpenCV detector with the same settings as before, to see what sort of detections we get.

In [50]:
# Convert the RGB  image to grayscale
gray_noise = cv2.cvtColor(image_with_noise, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray_noise, 4, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image_with_noise)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Noisy Image with Face Detections')
ax1.imshow(image_with_detections)
Number of faces detected: 11
Out[50]:
<matplotlib.image.AxesImage at 0x7fc36beef6d8>

With this added noise we now miss one of the faces!

(IMPLEMENTATION) De-noise this image for better face detection

Time to get your hands dirty: using OpenCV's built in color image de-noising functionality called fastNlMeansDenoisingColored - de-noise this image enough so that all the faces in the image are properly detected. Once you have cleaned the image in the next cell, use the cell that follows to run our trained face detector over the cleaned image to check out its detections.

You can find its official documentation here and a useful example here.

Note: you can keep all parameters except photo_render fixed as shown in the second link above. Play around with the value of this parameter - see how it affects the resulting cleaned image.

In [51]:
## TODO: Use OpenCV's built in color image de-noising function to clean up our noisy image!
fast_mean = cv2.fastNlMeansDenoisingColored(image_with_noise,None,10,7,7,18)

plt.imshow(fast_mean)

denoised_image = fast_mean # your final de-noised image (should be RGB)
In [52]:
## TODO: Run the face detector on the de-noised image to improve your detections and display the result
# Convert the RGB  image to grayscale
gray_noise = cv2.cvtColor(denoised_image, cv2.COLOR_RGB2GRAY)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
faces = face_cascade.detectMultiScale(gray_noise, 4, 6)

# Print the number of faces detected in the image
print('Number of faces detected:', len(faces))

# Make a copy of the orginal image to draw face detections on
image_with_detections = np.copy(image_with_noise)

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    cv2.rectangle(image_with_detections, (x,y), (x+w,y+h), (255,0,0), 3)
    

# Display the image with the detections
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Noisy Image with Face Detections')
ax1.imshow(image_with_detections)
Number of faces detected: 13
Out[52]:
<matplotlib.image.AxesImage at 0x7fc36be55e80>

Step 3: Blur an Image and Perform Edge Detection

Now that we have developed a simple pipeline for detecting faces using OpenCV - let's start playing around with a few fun things we can do with all those detected faces!

Importance of Blur in Edge Detection

Edge detection is a concept that pops up almost everywhere in computer vision applications, as edge-based features (as well as features built on top of edges) are often some of the best features for e.g., object detection and recognition problems.

Edge detection is a dimension reduction technique - by keeping only the edges of an image we get to throw away a lot of non-discriminating information. And typically the most useful kind of edge-detection is one that preserves only the important, global structures (ignoring local structures that aren't very discriminative). So removing local structures / retaining global structures is a crucial pre-processing step to performing edge detection in an image, and blurring can do just that.

Below is an animated gif showing the result of an edge-detected cat taken from Wikipedia, where the image is gradually blurred more and more prior to edge detection. When the animation begins you can't quite make out what it's a picture of, but as the animation evolves and local structures are removed via blurring the cat becomes visible in the edge-detected image.

Edge detection is a convolution performed on the image itself, and you can read about Canny edge detection on this OpenCV documentation page.

Canny edge detection

In the cell below we load in a test image, then apply Canny edge detection on it. The original image is shown on the left panel of the figure, while the edge-detected version of the image is shown on the right. Notice how the result looks very busy - there are too many little details preserved in the image before it is sent to the edge detector. When applied in computer vision applications, edge detection should preserve global structure; doing away with local structures that don't help describe what objects are in the image.

In [11]:
# Load in the image
image = cv2.imread('images/fawzia.jpg')

# Convert to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)  

# Perform Canny edge detection
edges = cv2.Canny(gray,100,200)

# Dilate the image to amplify edges
edges = cv2.dilate(edges, None)

# Plot the RGB and edge-detected image
fig = plt.figure(figsize = (15,15))
ax1 = fig.add_subplot(121)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Original Image')
ax1.imshow(image)

ax2 = fig.add_subplot(122)
ax2.set_xticks([])
ax2.set_yticks([])

ax2.set_title('Canny Edges')
ax2.imshow(edges, cmap='gray')
Out[11]:
<matplotlib.image.AxesImage at 0x7fc37aa0e0b8>

Without first blurring the image, and removing small, local structures, a lot of irrelevant edge content gets picked up and amplified by the detector (as shown in the right panel above).

(IMPLEMENTATION) Blur the image then perform edge detection

In the next cell, you will repeat this experiment - blurring the image first to remove these local structures, so that only the important boudnary details remain in the edge-detected image.

Blur the image by using OpenCV's filter2d functionality - which is discussed in this documentation page - and use an averaging kernel of width equal to 4.

In [72]:
### TODO: Blur the test imageusing OpenCV's filter2d functionality, 
# Use an averaging kernel, and a kernel width equal to 4
blured = cv2.filter2D(gray, -1, np.ones((4,4))/(4*4))

## TODO: Then perform Canny edge detection and display the output
# Perform Canny edge detection
edges = cv2.Canny(blured,100,200)

# Dilate the image to amplify edges
edges = cv2.dilate(edges, None)

# Plot the RGB and edge-detected image
fig = plt.figure(figsize = (15,15))
ax1 = fig.add_subplot(121)
ax1.set_xticks([])
ax1.set_yticks([])

ax1.set_title('Blured Image')
ax1.imshow(blured, cmap='gray')

ax2 = fig.add_subplot(122)
ax2.set_xticks([])
ax2.set_yticks([])

ax2.set_title('Canny Edges')
ax2.imshow(edges, cmap='gray')
Out[72]:
<matplotlib.image.AxesImage at 0x7fc36bb61748>

Step 4: Automatically Hide the Identity of an Individual

If you film something like a documentary or reality TV, you must get permission from every individual shown on film before you can show their face, otherwise you need to blur it out - by blurring the face a lot (so much so that even the global structures are obscured)! This is also true for projects like Google's StreetView maps - an enormous collection of mapping images taken from a fleet of Google vehicles. Because it would be impossible for Google to get the permission of every single person accidentally captured in one of these images they blur out everyone's faces, the detected images must automatically blur the identity of detected people. Here's a few examples of folks caught in the camera of a Google street view vehicle.

Read in an image to perform identity detection

Let's try this out for ourselves. Use the face detection pipeline built above and what you know about using the filter2D to blur and image, and use these in tandem to hide the identity of the person in the following image - loaded in and printed in the next cell.

In [13]:
# Load in the image
image = cv2.imread('images/gus.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[13]:
<matplotlib.image.AxesImage at 0x7fc37a987390>

(IMPLEMENTATION) Use blurring to hide the identity of an individual in an image

The idea here is to 1) automatically detect the face in this image, and then 2) blur it out! Make sure to adjust the parameters of the averaging blur filter to completely obscure this person's identity.

In [14]:
## TODO: Implement face detection
partial_blured_image = np.copy(image)

# Extract the pre-trained face detector from an xml file
face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')

# Detect the faces in image
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.5, 4)

## TODO: Blur the bounding box around each detected face using an averaging filter and display the result

# Get the bounding box for each detected face
for (x,y,w,h) in faces:
    # Add a red bounding box to the detections image
    partial_blured_image[y-70:y+h+20, x-20:x+w+20] = cv2.filter2D(image[y-70:y+h+20, x-20:x+w+20], -1, 
                                np.array(np.ones([100,100])/(100*100)))
    

plt.imshow(partial_blured_image)
Out[14]:
<matplotlib.image.AxesImage at 0x7fc389941940>

(Optional) Build identity protection into your laptop camera

In this optional task you can add identity protection to your laptop camera, using the previously completed code where you added face detection to your laptop camera - and the task above. You should be able to get reasonable results with little parameter tuning - like the one shown in the gif below.

As with the previous video task, to make this perfect would require significant effort - so don't strive for perfection here, strive for reasonable quality.

The next cell contains code a wrapper function called laptop_camera_identity_hider that - when called - will activate your laptop's camera. You need to place the relevant face detection and blurring code developed above in this function in order to blur faces entering your laptop camera's field of view.

Before adding anything to the function you can call it to get a hang of how it works - a small window will pop up showing you the live feed from your camera, you can press any key to close this window.

Note: Mac users may find that activating this function kills the kernel of their notebook every once in a while. If this happens to you, just restart your notebook's kernel, activate cell(s) containing any crucial import statements, and you'll be good to go!

In [ ]:
### Insert face detection and blurring code into the wrapper below to create an identity protector on your laptop!
import cv2
import time 

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # Exit by pressing any key
            # Destroy windows
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [ ]:
# Run laptop identity hider
laptop_camera_go()

Step 5: Create a CNN to Recognize Facial Keypoints

OpenCV is often used in practice with other machine learning and deep learning libraries to produce interesting results. In this stage of the project you will create your own end-to-end pipeline - employing convolutional networks in keras along with OpenCV - to apply a "selfie" filter to streaming video and images.

You will start by creating and then training a convolutional network that can detect facial keypoints in a small dataset of cropped images of human faces. We then guide you towards OpenCV to expanding your detection algorithm to more general images. What are facial keypoints? Let's take a look at some examples.

Facial keypoints (also called facial landmarks) are the small blue-green dots shown on each of the faces in the image above - there are 15 keypoints marked in each image. They mark important areas of the face - the eyes, corners of the mouth, the nose, etc. Facial keypoints can be used in a variety of machine learning applications from face and emotion recognition to commercial applications like the image filters popularized by Snapchat.

Below we illustrate a filter that, using the results of this section, automatically places sunglasses on people in images (using the facial keypoints to place the glasses correctly on each face). Here, the facial keypoints have been colored lime green for visualization purposes.

Make a facial keypoint detector

But first things first: how can we make a facial keypoint detector? Well, at a high level, notice that facial keypoint detection is a regression problem. A single face corresponds to a set of 15 facial keypoints (a set of 15 corresponding $(x, y)$ coordinates, i.e., an output point). Because our input data are images, we can employ a convolutional neural network to recognize patterns in our images and learn how to identify these keypoint given sets of labeled data.

In order to train a regressor, we need a training set - a set of facial image / facial keypoint pairs to train on. For this we will be using this dataset from Kaggle. We've already downloaded this data and placed it in the data directory. Make sure that you have both the training and test data files. The training dataset contains several thousand $96 \times 96$ grayscale images of cropped human faces, along with each face's 15 corresponding facial keypoints (also called landmarks) that have been placed by hand, and recorded in $(x, y)$ coordinates. This wonderful resource also has a substantial testing set, which we will use in tinkering with our convolutional network.

To load in this data, run the Python cell below - notice we will load in both the training and testing sets.

The load_data function is in the included utils.py file.

In [15]:
from utils import *

# Load training set
X_train, y_train = load_data()
print("X_train.shape == {}".format(X_train.shape))
print("y_train.shape == {}; y_train.min == {:.3f}; y_train.max == {:.3f}".format(
    y_train.shape, y_train.min(), y_train.max()))

# Load testing set
X_test, _ = load_data(test=True)
print("X_test.shape == {}".format(X_test.shape))
Using TensorFlow backend.
X_train.shape == (2140, 96, 96, 1)
y_train.shape == (2140, 30); y_train.min == -0.920; y_train.max == 0.996
X_test.shape == (1783, 96, 96, 1)

The load_data function in utils.py originates from this excellent blog post, which you are strongly encouraged to read. Please take the time now to review this function. Note how the output values - that is, the coordinates of each set of facial landmarks - have been normalized to take on values in the range $[-1, 1]$, while the pixel values of each input point (a facial image) have been normalized to the range $[0,1]$.

Note: the original Kaggle dataset contains some images with several missing keypoints. For simplicity, the load_data function removes those images with missing labels from the dataset. As an optional extension, you are welcome to amend the load_data function to include the incomplete data points.

Visualize the Training Data

Execute the code cell below to visualize a subset of the training data.

In [16]:
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_train[i], y_train[i], ax)

For each training image, there are two landmarks per eyebrow (four total), three per eye (six total), four for the mouth, and one for the tip of the nose.

Review the plot_data function in utils.py to understand how the 30-dimensional training labels in y_train are mapped to facial locations, as this function will prove useful for your pipeline.

(IMPLEMENTATION) Specify the CNN Architecture

In this section, you will specify a neural network for predicting the locations of facial keypoints. Use the code cell below to specify the architecture of your neural network. We have imported some layers that you may find useful for this task, but if you need to use more Keras layers, feel free to import them in the cell.

Your network should accept a $96 \times 96$ grayscale image as input, and it should output a vector with 30 entries, corresponding to the predicted (horizontal and vertical) locations of 15 facial keypoints. If you are not sure where to start, you can find some useful starting architectures in this blog, but you are not permitted to copy any of the architectures that you find online.

In [17]:
# Import deep learning resources from Keras
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, Dropout
from keras.layers import Flatten, Dense


## TODO: Specify a CNN architecture
# Your model should accept 96x96 pixel graysale images in
# It should have a fully-connected output layer with 30 values (2 for each facial keypoint)

model = Sequential([
    Convolution2D(filters=32, kernel_size=4, strides=1, activation='relu', padding='same', input_shape=(96, 96, 1)),
#     MaxPooling2D(),
    Convolution2D(filters=64, kernel_size=3, strides=1, activation='relu', padding='same'),
    MaxPooling2D(),
    Convolution2D(filters=64, kernel_size=3, strides=1, activation='relu', padding='same'),
    MaxPooling2D(),
    Flatten(),
    Dense(1024, activation='relu'),
    Dropout(.4),
    Dense(512, activation='relu'),
    Dropout(.4),
    Dense(512, activation='relu'),
    Dense(30),
])


# Summarize the model
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 96, 96, 32)        544       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 96, 96, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 48, 48, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 48, 48, 64)        36928     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 24, 24, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 36864)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              37749760  
_________________________________________________________________
dropout_1 (Dropout)          (None, 1024)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               524800    
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 512)               262656    
_________________________________________________________________
dense_4 (Dense)              (None, 30)                15390     
=================================================================
Total params: 38,608,574
Trainable params: 38,608,574
Non-trainable params: 0
_________________________________________________________________

Step 6: Compile and Train the Model

After specifying your architecture, you'll need to compile and train the model to detect facial keypoints'

(IMPLEMENTATION) Compile and Train the Model

Use the compile method to configure the learning process. Experiment with your choice of optimizer; you may have some ideas about which will work best (SGD vs. RMSprop, etc), but take the time to empirically verify your theories.

Use the fit method to train the model. Break off a validation set by setting validation_split=0.2. Save the returned History object in the history variable.

Experiment with your model to minimize the validation loss (measured as mean squared error). A very good model will achieve about 0.0015 loss (though it's possible to do even better). When you have finished training, save your model as an HDF5 file with file path my_model.h5.

In [18]:
from keras.optimizers import SGD, RMSprop, Adagrad, Adadelta, Adam, Adamax, Nadam
from keras.models import clone_model
models = {}

for optimizer in ["adagrad", "rmsprop", "adam"]:
    model_copy = clone_model(model)
    model_copy.set_weights(model.get_weights())

    ## TODO: Compile the model
    model_copy.compile( optimizer="adagrad", loss='mse' )

    ## TODO: Train the model
    hist = model_copy.fit( X_train, y_train, epochs=60, batch_size=32, validation_split=0.2)

    ## TODO: Save the model as model.h5
    model_copy.save('my_model_'+optimizer+'.h5')
    
    models[optimizer] = {
        "model": model_copy,
        "hist": hist
    }
Train on 1712 samples, validate on 428 samples
Epoch 1/60
1712/1712 [==============================] - 5s 3ms/step - loss: 2260.0311 - val_loss: 0.0059
Epoch 2/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0082 - val_loss: 0.0050
Epoch 3/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0068 - val_loss: 0.0044
Epoch 4/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0060 - val_loss: 0.0043
Epoch 5/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0055 - val_loss: 0.0042
Epoch 6/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0054 - val_loss: 0.0042
Epoch 7/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0051 - val_loss: 0.0042
Epoch 8/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0048 - val_loss: 0.0041
Epoch 9/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0046 - val_loss: 0.0043
Epoch 10/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0045 - val_loss: 0.0043
Epoch 11/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0048 - val_loss: 0.0041
Epoch 12/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0045 - val_loss: 0.0038
Epoch 13/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0041 - val_loss: 0.0035
Epoch 14/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0039 - val_loss: 0.0033
Epoch 15/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0035 - val_loss: 0.0031
Epoch 16/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0033 - val_loss: 0.0029
Epoch 17/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0031 - val_loss: 0.0027
Epoch 18/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0029 - val_loss: 0.0027
Epoch 19/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0028 - val_loss: 0.0032
Epoch 20/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0026 - val_loss: 0.0023
Epoch 21/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0022
Epoch 22/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0022
Epoch 23/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0023
Epoch 24/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0022 - val_loss: 0.0021
Epoch 25/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0022 - val_loss: 0.0022
Epoch 26/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0020
Epoch 27/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0021
Epoch 28/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0020 - val_loss: 0.0019
Epoch 29/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0019 - val_loss: 0.0019
Epoch 30/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0019 - val_loss: 0.0018
Epoch 31/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0019 - val_loss: 0.0018
Epoch 32/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0018 - val_loss: 0.0018
Epoch 33/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0018 - val_loss: 0.0018
Epoch 34/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0018 - val_loss: 0.0017
Epoch 35/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0017 - val_loss: 0.0017
Epoch 36/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0017 - val_loss: 0.0017
Epoch 37/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0017 - val_loss: 0.0016
Epoch 38/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0016 - val_loss: 0.0017
Epoch 39/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0016 - val_loss: 0.0016
Epoch 40/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0016 - val_loss: 0.0016
Epoch 41/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0015 - val_loss: 0.0016
Epoch 42/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0015 - val_loss: 0.0016
Epoch 43/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0015 - val_loss: 0.0017
Epoch 44/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0015 - val_loss: 0.0015
Epoch 45/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0014 - val_loss: 0.0015
Epoch 46/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0014 - val_loss: 0.0015
Epoch 47/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0014 - val_loss: 0.0015
Epoch 48/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0014 - val_loss: 0.0015
Epoch 49/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0014 - val_loss: 0.0015
Epoch 50/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0015
Epoch 51/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 52/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 53/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 54/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 55/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 56/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0013 - val_loss: 0.0014
Epoch 57/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0012 - val_loss: 0.0014
Epoch 58/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0012 - val_loss: 0.0014
Epoch 59/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0012 - val_loss: 0.0014
Epoch 60/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0012 - val_loss: 0.0014
Train on 1712 samples, validate on 428 samples
Epoch 1/60
1712/1712 [==============================] - 4s 2ms/step - loss: 1662.5776 - val_loss: 0.0092
Epoch 2/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.2828 - val_loss: 0.0138
Epoch 3/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0118 - val_loss: 0.0064
Epoch 4/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0089 - val_loss: 0.0082
Epoch 5/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0077 - val_loss: 0.0090
Epoch 6/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0074 - val_loss: 0.0061
Epoch 7/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0071 - val_loss: 0.0068
Epoch 8/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0069 - val_loss: 0.0112
Epoch 9/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0068 - val_loss: 0.0058
Epoch 10/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0064 - val_loss: 0.0062
Epoch 11/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0063 - val_loss: 0.0055
Epoch 12/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0061 - val_loss: 0.0050
Epoch 13/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0058 - val_loss: 0.0068
Epoch 14/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0057 - val_loss: 0.0065
Epoch 15/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0057 - val_loss: 0.0103
Epoch 16/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0056 - val_loss: 0.0059
Epoch 17/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0055 - val_loss: 0.0059
Epoch 18/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0053 - val_loss: 0.0066
Epoch 19/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0052 - val_loss: 0.0079
Epoch 20/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0053 - val_loss: 0.0070
Epoch 21/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0051 - val_loss: 0.0054
Epoch 22/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0051 - val_loss: 0.0089
Epoch 23/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0050 - val_loss: 0.0068
Epoch 24/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0050 - val_loss: 0.0076
Epoch 25/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0049 - val_loss: 0.0067
Epoch 26/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0048 - val_loss: 0.0078
Epoch 27/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0048 - val_loss: 0.0071
Epoch 28/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0047 - val_loss: 0.0082
Epoch 29/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0046 - val_loss: 0.0068
Epoch 30/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0046 - val_loss: 0.0079
Epoch 31/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0044 - val_loss: 0.0063
Epoch 32/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0044 - val_loss: 0.0063
Epoch 33/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0043 - val_loss: 0.0073
Epoch 34/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0042 - val_loss: 0.0078
Epoch 35/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0042 - val_loss: 0.0071
Epoch 36/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0041 - val_loss: 0.0067
Epoch 37/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0040 - val_loss: 0.0069
Epoch 38/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0039 - val_loss: 0.0060
Epoch 39/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0037 - val_loss: 0.0057
Epoch 40/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0037 - val_loss: 0.0063
Epoch 41/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0035 - val_loss: 0.0053
Epoch 42/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0034 - val_loss: 0.0052
Epoch 43/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0033 - val_loss: 0.0056
Epoch 44/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0032 - val_loss: 0.0039
Epoch 45/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0031 - val_loss: 0.0044
Epoch 46/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0030 - val_loss: 0.0046
Epoch 47/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0029 - val_loss: 0.0041
Epoch 48/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0029 - val_loss: 0.0038
Epoch 49/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0028 - val_loss: 0.0036
Epoch 50/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0027 - val_loss: 0.0041
Epoch 51/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0027 - val_loss: 0.0044
Epoch 52/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0026 - val_loss: 0.0042
Epoch 53/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0026 - val_loss: 0.0036
Epoch 54/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0039
Epoch 55/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0039
Epoch 56/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0034
Epoch 57/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0031
Epoch 58/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0029
Epoch 59/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0030
Epoch 60/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0023 - val_loss: 0.0031
Train on 1712 samples, validate on 428 samples
Epoch 1/60
1712/1712 [==============================] - 4s 2ms/step - loss: 1081.8099 - val_loss: 0.0228
Epoch 2/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0133 - val_loss: 0.0082
Epoch 3/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0094 - val_loss: 0.0114
Epoch 4/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0080 - val_loss: 0.0082
Epoch 5/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0074 - val_loss: 0.0106
Epoch 6/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0067 - val_loss: 0.0088
Epoch 7/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0064 - val_loss: 0.0066
Epoch 8/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0062 - val_loss: 0.0072
Epoch 9/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0058 - val_loss: 0.0053
Epoch 10/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0056 - val_loss: 0.0069
Epoch 11/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0053 - val_loss: 0.0080
Epoch 12/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0053 - val_loss: 0.0063
Epoch 13/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0052 - val_loss: 0.0069
Epoch 14/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0050 - val_loss: 0.0057
Epoch 15/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0049 - val_loss: 0.0058
Epoch 16/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0048 - val_loss: 0.0061
Epoch 17/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0047 - val_loss: 0.0074
Epoch 18/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0045 - val_loss: 0.0061
Epoch 19/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0043 - val_loss: 0.0079
Epoch 20/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0043 - val_loss: 0.0075
Epoch 21/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0041 - val_loss: 0.0046
Epoch 22/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0039 - val_loss: 0.0049
Epoch 23/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0039 - val_loss: 0.0050
Epoch 24/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0038 - val_loss: 0.0053
Epoch 25/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0037 - val_loss: 0.0052
Epoch 26/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0036 - val_loss: 0.0054
Epoch 27/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0035 - val_loss: 0.0051
Epoch 28/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0034 - val_loss: 0.0050
Epoch 29/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0033 - val_loss: 0.0047
Epoch 30/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0032 - val_loss: 0.0046
Epoch 31/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0031 - val_loss: 0.0039
Epoch 32/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0031 - val_loss: 0.0039
Epoch 33/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0030 - val_loss: 0.0048
Epoch 34/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0030 - val_loss: 0.0039
Epoch 35/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0029 - val_loss: 0.0036
Epoch 36/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0029 - val_loss: 0.0036
Epoch 37/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0028 - val_loss: 0.0041
Epoch 38/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0027 - val_loss: 0.0039
Epoch 39/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0027 - val_loss: 0.0038
Epoch 40/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0026 - val_loss: 0.0038
Epoch 41/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0026 - val_loss: 0.0036
Epoch 42/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0031
Epoch 43/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0025 - val_loss: 0.0038
Epoch 44/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0032
Epoch 45/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0033
Epoch 46/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0024 - val_loss: 0.0030
Epoch 47/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0023 - val_loss: 0.0033
Epoch 48/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0023 - val_loss: 0.0034
Epoch 49/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0023 - val_loss: 0.0031
Epoch 50/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0023 - val_loss: 0.0028
Epoch 51/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0022 - val_loss: 0.0029
Epoch 52/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0022 - val_loss: 0.0030
Epoch 53/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0027
Epoch 54/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0028
Epoch 55/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0023
Epoch 56/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0021 - val_loss: 0.0024
Epoch 57/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0020 - val_loss: 0.0025
Epoch 58/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0020 - val_loss: 0.0025
Epoch 59/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0020 - val_loss: 0.0026
Epoch 60/60
1712/1712 [==============================] - 3s 2ms/step - loss: 0.0020 - val_loss: 0.0028

Step 7: Visualize the Loss and Test Predictions

(IMPLEMENTATION) Answer a few questions and visualize the loss

Question 1: Outline the steps you took to get to your final neural network architecture and your reasoning at each step.

Answer: I started with a normal conv pool conv pool conv pool dense network, but then decided to add 3 extra fc layer with dropout to make it more general and robust. I then removed the first maxpool layer to try to prevent to much pixel precision loss, the removal of the max layer changed resulted in going from loss: 0.011 - val_loss: 0.0013 to loss: 8.2281e-04 - val_loss: 0.0012. Got gpu errors when i tried to remove the next pooling layer with 50 epoches. After plotting the loss graph, I saw the graph was not flatting out yet, so i gave it 10 epochs more.

Question 2: Defend your choice of optimizer. Which optimizers did you test, and how did you determine which worked best?

Answer: I start by using rms prop, and it converged well, then i experimented with sgd, adagrad, and adam. All of them did have around the same validation loss, but adagrad and adam had the lowest loss, adagrad had the nicest loss plot, it is easier to see when the loss is flatting out, it had the best loss as well, so I ended up choosing that one.

here is the loss table efter 50 epoches:

rms prop - loss: 8.2281e-04 - val_loss: 0.0012

sgd - loss: 6.4157e-04 - val_loss: 0.0010

adagrad - loss: 4.7840e-04 - val_loss: 0.0010

adam - loss: 4.7891e-04 - val_loss: 0.0011

Use the code cell below to plot the training and validation loss of your neural network. You may find this resource useful.

In [19]:
losses = {}
for key, model in models.items():
    losses[key+"_loss"] = model['hist'].history['loss']
    losses[key+"_val_loss"] = model['hist'].history['val_loss']
In [20]:
## TODO: Visualize the training and validation loss of your neural network
import pandas as pd
traning = pd.DataFrame.from_dict(losses)
traning.plot(logy=True, title="losses", ylim=(0,3e-2))
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_base.py:3245: UserWarning: Attempted to set non-positive ylimits for log-scale axis; invalid limits will be ignored.
  'Attempted to set non-positive ylimits for log-scale axis; '
Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fc2f09970b8>
In [22]:
model = models["adagrad"]["model"]

Question 3: Do you notice any evidence of overfitting or underfitting in the above plot? If so, what steps have you taken to improve your model? Note that slight overfitting or underfitting will not hurt your chances of a successful submission, as long as you have attempted some solutions towards improving your model (such as regularization, dropout, increased/decreased number of layers, etc).

Answer: I used a good amount of dropout, and the validation loss and normal loss did follow each other really nicely for my chosen optimizer, adagrad. But adam and rmsprop's validation loss is significantly higher than the normal loss witch mean the model is overfitting.

Visualize a Subset of the Test Predictions

Execute the code cell below to visualize your model's predicted keypoints on a subset of the testing images.

In [23]:
y_test = model.predict(X_test)
fig = plt.figure(figsize=(20,20))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1, xticks=[], yticks=[])
    plot_data(X_test[i], y_test[i], ax)

Step 8: Complete the pipeline

With the work you did in Sections 1 and 2 of this notebook, along with your freshly trained facial keypoint detector, you can now complete the full pipeline. That is given a color image containing a person or persons you can now

  • Detect the faces in this image automatically using OpenCV
  • Predict the facial keypoints in each face detected in the image
  • Paint predicted keypoints on each face detected

In this Subsection you will do just this!

(IMPLEMENTATION) Facial Keypoints Detector

Use the OpenCV face detection functionality you built in previous Sections to expand the functionality of your keypoints detector to color images with arbitrary size. Your function should perform the following steps

  1. Accept a color image.
  2. Convert the image to grayscale.
  3. Detect and crop the face contained in the image.
  4. Locate the facial keypoints in the cropped image.
  5. Overlay the facial keypoints in the original (color, uncropped) image.

Note: step 4 can be the trickiest because remember your convolutional network is only trained to detect facial keypoints in $96 \times 96$ grayscale images where each pixel was normalized to lie in the interval $[0,1]$, and remember that each facial keypoint was normalized during training to the interval $[-1,1]$. This means - practically speaking - to paint detected keypoints onto a test face you need to perform this same pre-processing to your candidate face - that is after detecting it you should resize it to $96 \times 96$ and normalize its values before feeding it into your facial keypoint detector. To be shown correctly on the original image the output keypoints from your detector then need to be shifted and re-normalized from the interval $[-1,1]$ to the width and height of your detected face.

When complete you should be able to produce example images like the one below

In [24]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')


# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_copy = np.copy(image)

# plot our image
fig = plt.figure(figsize = (9,9))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('image copy')
ax1.imshow(image_copy)
Out[24]:
<matplotlib.image.AxesImage at 0x7fc2e0207f98>
In [25]:
### TODO: Use the face detection code we saw in Section 1 with your trained conv-net 
## TODO : Paint the predicted keypoints on the test image

def draw_on_face(image):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(gray, 2, 5)

    for (x,y,w,h) in faces:
        # Add a red bounding box to the detections image
        cv2.rectangle(image, (x,y), (x+w,y+h), (255,0,0), 3)
        
        face_image = gray[y:y+h, x:x+w]
        face_image = cv2.resize(face_image, (96, 96)) 
        face_data = face_image.reshape((96,96,1))
        face_data = face_data / 255
        

        facial_key_points = model.predict(np.array([face_data]))[0]
        facial_key_points = facial_key_points * 48 + 48 # undo the normalization
        facial_key_points = facial_key_points * w / 96

        for index, point in enumerate(zip(facial_key_points[0::2], facial_key_points[1::2])):
            p_x = x + int(point[0]) 
            p_y = y + int(point[1]) 
            cv2.rectangle(image, (p_x-1,p_y-1), (p_x+1,p_y+1), (0,255,0), 3)

    return image


# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')


# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

image_copy=draw_on_face(np.copy(image))

# plot our image
fig = plt.figure(figsize = (9,9))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('image copy')
ax1.imshow(image_copy)
Out[25]:
<matplotlib.image.AxesImage at 0x7fc2e01be240>

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add facial keypoint detection to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for keypoint detection and marking in the previous exercise and you should be good to go!

In [ ]:
import cv2
import time 
from keras.models import load_model
def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # Try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # keep video stream open
    while rval:
        # plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # destroy windows
            cv2.destroyAllWindows()
            
            # hack from stack overflow for making sure window closes on osx --> https://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()  
In [ ]:
# Run your keypoint face painter
laptop_camera_go()

(Optional) Further Directions - add a filter using facial keypoints

Using your freshly minted facial keypoint detector pipeline you can now do things like add fun filters to a person's face automatically. In this optional exercise you can play around with adding sunglasses automatically to each individual's face in an image as shown in a demonstration image below.

To produce this effect an image of a pair of sunglasses shown in the Python cell below.

In [26]:
# Load in sunglasses image - note the usage of the special option
# cv2.IMREAD_UNCHANGED, this option is used because the sunglasses 
# image has a 4th channel that allows us to control how transparent each pixel in the image is
sunglasses = cv2.imread("images/sunglasses_4.png", cv2.IMREAD_UNCHANGED)

# Plot the image
fig = plt.figure(figsize = (6,6))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.imshow(sunglasses)
ax1.axis('off');

This image is placed over each individual's face using the detected eye points to determine the location of the sunglasses, and eyebrow points to determine the size that the sunglasses should be for each person (one could also use the nose point to determine this).

Notice that this image actually has 4 channels, not just 3.

In [27]:
# Print out the shape of the sunglasses image
print ('The sunglasses image has shape: ' + str(np.shape(sunglasses)))
The sunglasses image has shape: (1123, 3064, 4)

It has the usual red, blue, and green channels any color image has, with the 4th channel representing the transparency level of each pixel in the image. Here's how the transparency channel works: the lower the value, the more transparent the pixel will become. The lower bound (completely transparent) is zero here, so any pixels set to 0 will not be seen.

This is how we can place this image of sunglasses on someone's face and still see the area around of their face where the sunglasses lie - because these pixels in the sunglasses image have been made completely transparent.

Lets check out the alpha channel of our sunglasses image in the next Python cell. Note because many of the pixels near the boundary are transparent we'll need to explicitly print out non-zero values if we want to see them.

In [28]:
# Print out the sunglasses transparency (alpha) channel
alpha_channel = sunglasses[:,:,3]
print ('the alpha channel here looks like')
print (alpha_channel)

# Just to double check that there are indeed non-zero values
# Let's find and print out every value greater than zero
values = np.where(alpha_channel != 0)
print ('\n the non-zero values of the alpha channel look like')
print (values)
the alpha channel here looks like
[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 0]]

 the non-zero values of the alpha channel look like
(array([  17,   17,   17, ..., 1109, 1109, 1109]), array([ 687,  688,  689, ..., 2376, 2377, 2378]))

This means that when we place this sunglasses image on top of another image, we can use the transparency channel as a filter to tell us which pixels to overlay on a new image (only the non-transparent ones with values greater than zero).

One last thing: it's helpful to understand which keypoint belongs to the eyes, mouth, etc. So, in the image below, we also display the index of each facial keypoint directly on the image so that you can tell which keypoints are for the eyes, eyebrows, etc.

With this information, you're well on your way to completing this filtering task! See if you can place the sunglasses automatically on the individuals in the image loaded in / shown in the next Python cell.

In [29]:
# Load in color image for face detection
image = cv2.imread('images/obamas4.jpg')

# Convert the image to RGB colorspace
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


# Plot the image
fig = plt.figure(figsize = (8,8))
ax1 = fig.add_subplot(111)
ax1.set_xticks([])
ax1.set_yticks([])
ax1.set_title('Original Image')
ax1.imshow(image)
Out[29]:
<matplotlib.image.AxesImage at 0x7fc2e0185da0>
In [30]:
## (Optional) TODO: Use the face detection code we saw in Section 1 with your trained conv-net to put
## sunglasses on the individuals in our test image
def draw_object_on_eyes(image, overlay):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    face_cascade = cv2.CascadeClassifier('detector_architectures/haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(gray, 2, 5)

    make_bigger_by = 35
    
    for (x,y,w,h) in faces:   
        
        face_image = gray[y:y+h, x:x+w]
        face_image = cv2.resize(face_image, (96, 96)) 
        face_data = face_image.reshape((96,96,1))
        face_data = face_data / 255
        

        facial_key_points = model.predict(np.array([face_data]))[0]
        facial_key_points = facial_key_points * 48 + 48 # undo the normalization
        facial_key_points = facial_key_points * w / 96
        points = list(zip(facial_key_points[0::2], facial_key_points[1::2]))
        
        x_inner, y_inner = int(points[9][0]-(make_bigger_by/2)), int(points[9][1])
        width = int(points[7][0] - points[9][0]) + make_bigger_by
        height = int(overlay.shape[0] * (width / overlay.shape[1]))
        
        small_overlay = np.copy(overlay)
        small_overlay = cv2.resize(small_overlay, (width, height))
        
        # time to get dirty
        for obj_x in range(width):
            for obj_y in range(height):
                pixel = small_overlay[obj_y][obj_x]
                if pixel[3] > 125:
                    image[y+y_inner+obj_y][x+x_inner+obj_x] = pixel[0:3]

    
    return image

plt.imshow(draw_object_on_eyes(np.copy(image), sunglasses))
Out[30]:
<matplotlib.image.AxesImage at 0x7fc2e0139160>

(Optional) Further Directions - add a filter using facial keypoints to your laptop camera

Now you can add the sunglasses filter to your laptop camera - as illustrated in the gif below.

The next Python cell contains the basic laptop video camera function used in the previous optional video exercises. Combine it with the functionality you developed for adding sunglasses to someone's face in the previous optional exercise and you should be good to go!

In [ ]:
import cv2
import time 
from keras.models import load_model
import numpy as np

def laptop_camera_go():
    # Create instance of video capturer
    cv2.namedWindow("face detection activated")
    vc = cv2.VideoCapture(0)

    # try to get the first frame
    if vc.isOpened(): 
        rval, frame = vc.read()
    else:
        rval = False
    
    # Keep video stream open
    while rval:
        # Plot image from camera with detections marked
        cv2.imshow("face detection activated", frame)
        
        # Exit functionality - press any key to exit laptop video
        key = cv2.waitKey(20)
        if key > 0: # exit by pressing any key
            # Destroy windows 
            cv2.destroyAllWindows()
            
            for i in range (1,5):
                cv2.waitKey(1)
            return
        
        # Read next frame
        time.sleep(0.05)             # control framerate for computation - default 20 frames per sec
        rval, frame = vc.read()    
        
In [ ]:
# Load facial landmark detector model
model = load_model('my_model.h5')

# Run sunglasses painter
laptop_camera_go()